/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pl.controller;

import java.io.IOException;
import java.util.Collection;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import pl.model.dao.AuthorDao;
import pl.model.domain.Author;
import pl.model.domain.Book;

/**
 *
 * @author jakub
 */
@Controller
public class AuthorController {

    protected AuthorDao authorDao;
    
    protected final Log log = LogFactory.getLog(getClass());

    @RequestMapping("/addAuthor.htm")
    public ModelMap showaddAuthor() {
        System.out.println("addAuthor - show");
        return new ModelMap();
    }

    @RequestMapping("/doAddAuthor.htm")
    public ModelAndView addAuthor(@RequestParam("name") String name,
            @RequestParam("surname") String surname) {

        Author author = new Author(name, surname);

        try {
            System.out.println("przed dodaniu " + author);
            authorDao.persist(author);
            System.out.println("po dodaniu " + author);
            ModelAndView mav = new ModelAndView("redirect:listAuthors.htm");
            System.out.println("dodal autora " + author);
            return mav;
        } catch (DataIntegrityViolationException e) {
            e.printStackTrace();
            return new ModelAndView("addAuthor", "err", "ingegrity: " + e.getMessage());
        } catch (DataAccessException e) {
            e.printStackTrace();
            return new ModelAndView("addAuthor", "err", "dataaccess: " + e.getMessage());
        }

    }

    @RequestMapping("/listAuthors.htm")
    public ModelMap listAuthors() {
         System.out.println("listAuthor - show " + authorDao.getAll().size());
   //     return new ModelAndView("listAuthors", "authors", authorDao.getAll());
         return new ModelMap("authors", authorDao.getAll());
    }

    @RequestMapping("/authorDetails.htm")
    public ModelAndView authorDetails(@RequestParam int authorId) {
         System.out.println("authorDetails - show");
        return new ModelAndView("authorDetails", "author", authorDao.find(authorId));
    }

    @RequestMapping("/listAuthorBooks.htm")
    public ModelMap listAuthorBooks(@RequestParam Integer authorId){
    	log.info("listAuthorBooks start");
    	
    	Collection<Book> books = authorDao.getAuthorBooks(authorId);
    	
    	log.info("got Books");
    	log.info("books size " + books.size());
    	log.info("first book " + books.iterator().next());
    	
    	return new ModelMap("books", books);
    }
    
    @RequestMapping("/testNoRollback.htm")
    /*
    @Transactional(noRollbackFor={
    		HibernateJdbcException.class, 
    		RollbackException.class, 
    		TransactionSystemException.class,
    		GenericJDBCException.class,
    		BatchUpdateException.class})
    		*/
    public ModelMap testNoRollback(HttpServletRequest request, HttpServletResponse response) throws IOException{
    	
    	log.info("before testNoRollback");
    	try {
			authorDao.testNoRollback();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			log.info("=================================1err=");
	//		e.printStackTrace();
		}
		
		try {
			authorDao.persist(new Author("pawel", "szymanski"));
			log.info("persisted first pawel");
			authorDao.persist(new Author("pawel", "szymanski"));
			log.info("persisted second pawel");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			log.info("=================================2err=" + e.getClass());
		//	e.printStackTrace();
		}
		try {
			authorDao.persist(new Author("staszek", "sochacki"));
			log.info("persisted staszek");
		} catch (Exception e) {
			log.info("=================================3err=");
		}
    	log.info("after testNoRollback");
    	response.getWriter().append("executed").flush();
		
    	return null;
    }
    
    public void setAuthorDao(AuthorDao authorDao) {
        this.authorDao = authorDao;
    }
}
